import json
import os
import sys
import time

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMainWindow, QTableWidgetItem, QFileDialog
from qtpy import uic

from share import SI, mySignals
import pyqtgraph as pg
class Monitor(QMainWindow):
    def __init__(self):
        super().__init__()
        # PyQt5
        self.ui = uic.loadUi("ui/monitor.ui")
        mySignals.statsRecved.connect(self.StatsRecevd)
        self.ui.splitter.setSizes([500, 400])
        #表格里面的数据
        self.statsInfoList = []
        for row in range(10):
            for col in range(7):
                item = QTableWidgetItem('')
                item.setFlags(Qt.ItemIsEnabled) # 参数名字段居中不允许修改
                item.setTextAlignment(Qt.AlignCenter)
                self.ui.tableWidget.setItem(row, col, item)

        self.SetPlotWidget()
        self.ui.action.triggered.connect(self.ActionTriggered)
    def SetPlotWidget(self):

        self.pw = pg.PlotWidget()     # 创建 PlotWidget 对象
        self.pw.setBackground('w')        # 背景色改为白色
        self.pw.showGrid(x=True, y=True)        # 显示表格线
        self.pw.setMouseEnabled(x=False,y=False)
        self.pw.setTitle("请求响应数量", color='#008080', size='12pt')        # 设置图表标题
        self.pw.setLabel("left", "单位(个)")        # 设置上下左右的label

        # 实时显示应该获取 PlotDataItem 对象, 调用其setData方法，
        # 这样只重新plot该曲线，性能更高
        self.pdi_rps = self.pw.plot(pen=pg.mkPen('r', width=1))
        self.pdi_tps = self.pw.plot(pen=pg.mkPen('g', width=1))


        # 创建 绘图对象2
        self.pw2 = pg.PlotWidget()
        self.pw2.setBackground('w')
        self.pw2.showGrid(x=True, y=True)
        self.pw2.setMouseEnabled(x=False,y=False)
        self.pw2.setTitle("响应时长",color='#008080',size='12pt')
        self.pw2.setLabel("left","单位：秒")
        self.pdi_latency = self.pw2.plot(pen=pg.mkPen('#4682B4'))

        # 添加 PlotWidget 对象到 界面layout中
        self.ui.layout.addWidget(self.pw)
        self.ui.layout.addWidget(self.pw2)

        self.plotData_xticks = []  #x轴坐标
        self.plotData_rps = []# 该秒发送请求数量
        self.plotData_tps = []# 该秒接收响应数量
        self.plotData_latency = []# 该秒平均响应时长

    def StatsRecevd(self, str):
        stats = json.loads(str)

        record = []

        timestamp = time.strftime('%M:%S', time.localtime(stats['t']))
        record.append(timestamp)

        rps = stats['rps']
        record.append(f"{rps} | {stats['total'].pop('send')}")

        tps = stats['tps']
        record.append(f"{tps} | {stats['total'].pop('recv')}")

        tops = stats['tops']
        total_timeout = stats['total'].pop('timeout') if 'timeout' in stats['total'] else 0
        record.append(f"{tops} | {total_timeout}")

        eps = stats['eps']
        total_error = stats['total'].pop('error') if 'error' in stats['total'] else 0
        record.append(f"{eps} | {total_error}")

        latency = stats['avgRespTime']
        record.append(f"{latency:.4f}")

        record.append(' | '.join([f'{k}:{v}' for k, v in stats['total'].items()]))

        self.statsInfoList.insert(0, record)

        # 曲线图数据保存
        self.plotData_xticks.append(timestamp)
        self.plotData_rps.append(rps)
        self.plotData_tps.append(tps)
        self.plotData_latency.append(latency)

        # 如果到达11次记录
        if len(self.statsInfoList) == 11:
            self.statsInfoList.pop(10)

            self.plotData_xticks.pop(0)
            self.plotData_rps.pop(0)
            self.plotData_tps.pop(0)
            self.plotData_latency.pop(0)

        # 更新表格
        for row in range(len(self.statsInfoList)):
            for col in range(7):
                self.ui.tableWidget.item(row, col).setText(self.statsInfoList[row][col])

        # 更新曲线图
        xTick = [[(idx, tick) for idx, tick in enumerate(self.plotData_xticks)]]

        xax = self.pw.getAxis('bottom')
        xax.setTicks(xTick)

        xserial = range(len(self.plotData_rps))

        self.pdi_rps.setData(xserial, self.plotData_rps)
        self.pdi_tps.setData(xserial, self.plotData_tps)

        xax2 = self.pw2.getAxis('bottom')
        xax2.setTicks(xTick)
        self.pdi_latency.setData(xserial, self.plotData_latency)

    def ActionTriggered(self):
        filePaths, _ = QFileDialog.getOpenFileNames(
            self.ui,  # 父窗口对象
            "选择数据文件",  # 标题
            SI.WorkFilePath,  # 起始目录
            "统计数据 (*.sts)"  # 选择类型过滤项，过滤内容在括号中
        )

        if not filePaths:
            return

        nl = ','.join([f"r'{fp}'" for fp in filePaths])
        code = f'''from hyload.tools.plotperf import ps; ps([{nl}])'''

        cmd = f'start call "{sys.executable}" -c "{code}"'
        self.LogInfo(f'作图: {cmd}')
        os.system(cmd)
        return
    def LogInfo(self, str):
        SI.LogInfo(str)